package com.ygq.Impl;

import DO.ClassesDO;
import DO.StudentDO;
import DTO.AddStudentDTO;
import DTO.EditStudentPasswordDTO;
import DTO.PageStudentDTO;
import DTO.StudentLoginDTO;
import Exceptions.BaseException;
import Exceptions.StudentAlreadyExistException;
import VO.StudentVO;
import com.ygq.StudentService;
import constant.PasswordConstant;
import mapper.ClassesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import mapper.studentMapper;
import org.springframework.transaction.annotation.Transactional;
import result.PageResult;
import utils.Student;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private studentMapper studentMapper;

    @Autowired
    private ClassesMapper classesMapper;

    /**
     * 学生登录
     * @param studentLoginDTO
     * @return
     */
    public StudentDO getById(StudentLoginDTO studentLoginDTO) {
        return studentMapper.studentLogin(studentLoginDTO);
    }

    /**
     * 学生分页查询
     * @param pageStudentDTO
     * @return
     */
    public PageResult page(PageStudentDTO pageStudentDTO) {
        /*
         * 这里将getCurrentPage作为 (currentPage - 1) * pageSize;
         */
        pageStudentDTO.setCurrentPage( (pageStudentDTO.getCurrentPage() - 1) * pageStudentDTO.getPageSize());
        /**
         * 两次查询，第一次为查询这个班级有多少个人
         */
        Integer total = studentMapper.getTotalBYId(pageStudentDTO);

        // 判断username是否为空，一旦不为空就只让他查第一页的数据
        if (!pageStudentDTO.getUsername().isEmpty()){
            if (total <= pageStudentDTO.getCurrentPage()){
                pageStudentDTO.setCurrentPage(0);
            }
        }

        List<StudentVO> studentVOS = studentMapper.pageQuery(pageStudentDTO);
        return new PageResult(total,studentVOS);
    }

    /**
     * 添加班级和添加该班级学生信息
     * @param students
     * @param className
     */
    @Transactional
    public void addClassAndStudent(List<Student> students, String className) {
        //因为在classes表中 班级名字加了唯一索引。因此在删除班级这里。要改变班级名称为    uuid+班级名称
        // 添加班级  获取当前时间
        ClassesDO classesDO = new ClassesDO();
        classesDO.setCreateTime(LocalDateTime.now());
        classesDO.setClassName(className);
        classesMapper.insert(classesDO);
        // 添加班级学生信息
        List<StudentDO> studentDoList = new ArrayList<>();
        for (Student student : students) {
            StudentDO studentDo = new StudentDO();
            studentDo.setStudentNumber(student.getStudentNumber());//学号
            studentDo.setUsername(student.getUsername());//姓名
            studentDo.setClassId(classesDO.getId());//班级id
            studentDo.setPassword(PasswordConstant.DEFAULT_PASSWORD);//设置默认密码
            studentDo.setCreateTime(LocalDateTime.now());//设置创建时间
            studentDoList.add(studentDo);
        }
        studentMapper.insert(studentDoList);
    }

    /**
     * 添加学生（1个）
     * @param addStudentDTO
     */
    public void addStudent(AddStudentDTO addStudentDTO) {
        // 第一步 ，查看这个学号有人没有
        StudentDO studentDo = studentMapper.getById(addStudentDTO.getStudentNumber());
        if (studentDo!=null){
            //此时抛出学生已经存在的异常
            throw new StudentAlreadyExistException();
        }
        // 第二步 向学生表添加信息
        StudentDO addStudentDo = new StudentDO();
        addStudentDo.setPassword(PasswordConstant.DEFAULT_PASSWORD);//密码
        addStudentDo.setStudentNumber(addStudentDTO.getStudentNumber());//学号
        addStudentDo.setUsername(addStudentDTO.getUsername());//姓名
        addStudentDo.setClassId(addStudentDTO.getClassId());//班级id
        addStudentDo.setCreateTime(LocalDateTime.now());//创建时间

        List<StudentDO> studentDoList = new ArrayList<>();
        studentDoList.add(addStudentDo);
        studentMapper.insert(studentDoList);
    }

    /**
     * 重置密码
     * @param id
     */
    @Transactional
    public void resetPassword(Integer id) {
        int count = studentMapper.resetPassword(id,PasswordConstant.DEFAULT_PASSWORD);
        if (count!=1){
            throw new BaseException();
        }
    }

    /**
     * 删除学生
     * @param id
     */
    public void deleteStudent(Integer id) {
        studentMapper.delectById(id);
    }

    // 根据id获取原密码
    public String getPasswordById(Integer id) {
        return studentMapper.getPasswordById(id);
    }

    // 修改密码
    public void editPassword(EditStudentPasswordDTO editStudentPasswordDTO) {
        studentMapper.editPassword(editStudentPasswordDTO);
    }


    // 根据班级id获取该班级的全部userId
    public List<Integer> getUserIdByClassId(Integer ClassId) {
        return studentMapper.getUserIds(ClassId);
    }


    public List<StudentDO> getByIdToTwo(Integer classId) {
        return studentMapper.getAllById(classId);
    }
}
